package com.pacific.uumconsole.web;

import com.pacific.uumconsole.biz.user.UserService;
import com.pacific.uumconsole.common.MD5;
import com.pacific.uumconsole.common.StringUtil;
import com.pacific.uumconsole.model.po.e8schema.mbg.AccountInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.nio.charset.Charset;
import java.util.Arrays;

/**
 * Created by admin on 2017-04-18.
 */
@Controller
public class BaseController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "tologin")
    public String login(){
        return "login/login";
    }

    @RequestMapping(value = "login",method= RequestMethod.POST)
    public String login(HttpServletRequest request, String username, String password,String autoLogin, HttpServletResponse response, HttpSession session, Model model){
        System.out.printf("teest");
        String errorMsg = "";
        if (username.equals("请输入用户名")||null == username || "".equals(username.trim())||password.equals("请输入密码")||null == password||"".equals(password.trim())) {
            errorMsg = "用户名或密码不能为空!";
            if (!errorMsg.isEmpty()) {
                model.addAttribute("errorMsg", errorMsg);
                return "login/login";
            }
        }
        AccountInfo account=userService.getAccountByName(username);
        byte [] inputPasswordhash = MD5.stringDecimalDigest(password).getBytes(Charset.forName("UTF-8"));
        if (null == account) {
            errorMsg = "用户名不存在!";
            if (!errorMsg.isEmpty()) {
                model.addAttribute("errorMsg", errorMsg);
                return "login/login";

            }
        }
        if (!Arrays.equals(inputPasswordhash, account.getPasswordHash())) {
            errorMsg = "密码错误!";
        }

        account.setName(username);

        //使用cookie保存用户信息
        Cookie userInfoCookie = new Cookie("userInfo", account.getId() + "-" + account.getName());
        userInfoCookie.setMaxAge(60*30); //userInfo 保存30min
        userInfoCookie.setPath("/");
        response.addCookie(userInfoCookie);


        if (autoLogin(autoLogin)) {
            //md5加密算法保存用户密码到cookie中
            Cookie pwdCookie = new Cookie(account.getName() + "password", MD5.stringDecimalDigest(password));
            pwdCookie.setPath("/");
            userInfoCookie.setMaxAge(60*60*24*7);  //userInfo 保存7天
            pwdCookie.setMaxAge(60*60*24*7); 	   //password 保存7天
            response.addCookie(pwdCookie);
        }

        if (!errorMsg.isEmpty()) {
            model.addAttribute("errorMsg", errorMsg);
            return "login/login";
        }
        session.setAttribute("userId", account.getUserId());
        session.setAttribute("username", username);//保存名字传入界面
        return "success";
    }
    private boolean autoLogin(String autoLoginStr){
        return !StringUtil.isEmpty(autoLoginStr) && 2 == Integer.parseInt(autoLoginStr);
    }
}
